#include "logic.h"
#include "stdlib.h"
#include "ms5803.h"
uint8_t u1Len = 0;
uint8_t u1Buff[256];
RTC_DateTypeDef gData;
RTC_TimeTypeDef gTime;
uint8_t gSrcAddr = 0;
uint16_t gBtValue = 0;
float   gPressVal;
float   gTemp = 0;
uint8_t    wakeFromPin = 0;
uint8_t    gSendFlag = 0;
uint16_t Power_ADC_Read(uint32_t timeout)
{   
    uint16_t adcTemp = 0;
    HAL_ADC_Start(&hadc);
    HAL_ADC_PollForConversion(&hadc, timeout);
    adcTemp = HAL_ADC_GetValue(&hadc);
    HAL_ADC_Stop(&hadc);
    gBtValue = (uint16_t)adcTemp*550/4096.0;
    return adcTemp;
}
void Cycle_Scure_Report()
{
    Record_Press();
    Power_ADC_Read(1000);
    printf("ACKBT:%d,%.2f#\r\n",gSrcAddr,gBtValue/100.0);
    printf("ACKPRESS:%d,%.2f,%.2f#\r\n",gSrcAddr,gPressVal,gTemp);
}
void Record_Press()
{
    gPressVal = MS5803_Get_Pressure();
    gTemp = MS5803_Get_Temperature();
}
void Get_Rtc_Time()
{
    if (HAL_RTC_GetTime(&hrtc, &gTime, RTC_FORMAT_BIN) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
    if (HAL_RTC_GetDate(&hrtc, &gData, RTC_FORMAT_BIN) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
}
void Set_Rtc_Time()
{
    RTC_DateTypeDef  sDataStructure;
    RTC_TimeTypeDef  sTimeStructure;

    /* Set Date: Sunday MAY 1th 2016 */
    sDataStructure.Year = 0x18;
    sDataStructure.Month = 0x05;
    sDataStructure.Date = 0x11;
    sDataStructure.WeekDay = RTC_WEEKDAY_SUNDAY;

    while(HAL_RTC_SetDate(&hrtc,&sDataStructure,RTC_FORMAT_BCD) != HAL_OK);

    /* Set Time: 02:20:00 */
    sTimeStructure.Hours = 0x00;
    sTimeStructure.Minutes = 0x00;
    sTimeStructure.Seconds = 0x00;
    sTimeStructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
    sTimeStructure.StoreOperation = RTC_STOREOPERATION_RESET;

    while(HAL_RTC_SetTime(&hrtc,&sTimeStructure,RTC_FORMAT_BCD) != HAL_OK);
}
void Set_AlarmA_Time(uint8_t perHour,uint8_t perMin,uint8_t perSec)
{
    uint8_t hour = 0;
    RTC_AlarmTypeDef sAlarm;
    if(perHour >= 24) hour = 24;
    else hour = perHour;
    sAlarm.AlarmTime.Hours = hour;
    sAlarm.AlarmTime.Minutes = perMin;
    sAlarm.AlarmTime.Seconds = perSec;
    sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
    sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
    sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
    sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
    sAlarm.AlarmDateWeekDay = 0x1;
    sAlarm.Alarm = RTC_ALARM_A;
    if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
    HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);

}
void Set_AlarmB_Time(uint8_t perHour,uint8_t perMin,uint8_t perSec)
{
    uint8_t hour = 0;
    RTC_AlarmTypeDef sBlarm;
    if(perHour >= 24) hour = 24;
    else hour = perHour;
    sBlarm.AlarmTime.Hours = hour;
    sBlarm.AlarmTime.Minutes = perMin;
    sBlarm.AlarmTime.Seconds = perSec;
    sBlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
    sBlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
    sBlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
    sBlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
    sBlarm.AlarmDateWeekDay = 0x1;
    sBlarm.Alarm = RTC_ALARM_B;
    if (HAL_RTC_SetAlarm_IT(&hrtc, &sBlarm, RTC_FORMAT_BIN) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
    HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
}
void Scan_MulBtn()
{
    GPIO_PinState tmp = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_11);
    gSrcAddr = tmp;
    tmp = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_10);
    gSrcAddr |= (tmp<<1);
    tmp = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9);
    gSrcAddr |= (tmp<<2);
    tmp = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_8);
    gSrcAddr |= (tmp<<3);
    
    tmp = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15);
    gSrcAddr |= (tmp<<4);
    tmp = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14);
    gSrcAddr |= (tmp<<5);
    tmp = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_13);
    gSrcAddr |= (tmp<<6);
    tmp = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12);
    gSrcAddr |= (tmp<<7);
    gSrcAddr = (~gSrcAddr);
}
void Enter_Stop_Mode()
{
   HAL_UART_MspDeInit(&huart1);
   HAL_ADC_MspDeInit(&hadc);
   MY_GPIO_DeInit();
   HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON,PWR_STOPENTRY_WFI);
}
void Exit_Stop_Mode()
{
    SystemClock_Config();
    HAL_UART_MspInit(&huart1);
    HAL_ADC_MspInit(&hadc);
    MY_GPIO_Init();
    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
}
void RTCA_Callback()
{
    Get_Rtc_Time();
    RTC_TimeTypeDef tmp;
    tmp.Seconds = gTime.Seconds;
    tmp.Minutes = gTime.Minutes;
    tmp.Hours = gTime.Hours;
    #ifndef SHORT_DEBUG //default not enable short debug  12hours wake up
    tmp.Hours += 6;
    if(tmp.Hours >= 24)
    {
        tmp.Hours -= 24;
    }
    #else    //debug mode per minutes wake up
    tmp.Seconds += 20;
    if(tmp.Seconds >= 60)
    {
        tmp.Seconds -= 60;
        tmp.Minutes++;
    }
    #endif
    Set_AlarmA_Time(tmp.Hours,tmp.Minutes,tmp.Seconds);
}